package carnet;
import java.util.ArrayList;

public class Serialisation {
	ArrayList<Carnet> carnets;
	
	public Serialisation(){
		carnets = new ArrayList<Carnet>();
	}
	
	public void addCarnet(Carnet c){
		carnets.add(c);
	}
	
	public String toXML(){
		String sXML = new String();
		sXML += "<Carnets>";
		for(Carnet c : carnets){
			sXML += "\t<Carnet type=\""+ c.type +"\"> \n";
			for(Contact ct : c.getContacts()){
				sXML += "\t\t<Contact " + ct.getNom().getNom() + "=\"" + ct.getNom().getValeur() +"\" ";
				sXML += ct.getPrenom().getNom() + "=\"" + ct.getPrenom().getValeur() +"\" ";
				sXML += ct.getAdresse().getNom() + "=\"" + ct.getAdresse().getValeur() +"\" ";
				sXML += ct.getTelephone().getNom() + "=\"" + ct.getTelephone().getValeur() +"\" ";
				for(Champ ch : ct.getAutresChamps()){
					sXML += ch.getNom() + "=\"" + ch.getValeur() +"\" ";
				}
				sXML += ">";
				sXML += "\t\t</Contact>";
			}
			sXML += "\t</Carnet>";
		}
		sXML += "</Carnets>";
		return sXML;
	}
	
	public String toTXT(){
		String sTXT = new String();
		for(Carnet c : carnets){
			sTXT += "Carnet "+ c.type +"\n";
			for(Contact ct : c.getContacts()){
				sTXT += ct.getNom().getValeur() +" ; ";
				sTXT += ct.getPrenom().getValeur() +" ; ";
				sTXT += ct.getAdresse().getValeur() +" ; ";
				sTXT += ct.getTelephone().getValeur() +" ; ";
				for(Champ ch : ct.getAutresChamps()){
					sTXT += ch.getValeur() +" ; ";
				}
			}
			sTXT += "\n";
		}
		return sTXT;
	}
}